import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useAuthStore = defineStore('auth', () => {
  const user = ref(null)
  const isAuthenticated = ref(false)

  const login = (credentials) => {
    // 这里应该是实际的登录逻辑，暂时模拟登录
    if (credentials.username && credentials.password) {
      user.value = { username: credentials.username }
      isAuthenticated.value = true
      localStorage.setItem('isAuthenticated', 'true')
    }
  }

  const logout = () => {
    user.value = null
    isAuthenticated.value = false
    localStorage.removeItem('isAuthenticated')
  }

  const checkAuth = () => {
    isAuthenticated.value = localStorage.getItem('isAuthenticated') === 'true'
  }

  return { user, isAuthenticated, login, logout, checkAuth }
})